package com.zx.chatroom.server;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.SocketIOServer;
import com.zx.chatroom.listener.HallListener;
import com.zx.chatroom.listener.TalkListener;
import com.zx.framework.core.util.SpringBeanUtil;
import com.zx.framework.mina.MinaServer;
import org.apache.log4j.Logger;

import java.util.Collection;

public class SocketServer {
	
	public static final String HALL = "hall";
	/**
	 * 聊天室
	 */
	public static final String TALK = "talk";
	
	private static SocketServer instance;
	
	private SocketServer() {
	}

	private static final Logger LOGGER = Logger.getLogger(SocketServer.class);
	private SocketIOServer socketIOServer;
	private int bindport = -1;
	private String bindhost="";
    private String visithost="";
	private static String charInfoOutPath;
	
	
	public static SocketServer getInstance() {
		if (instance == null) {
			synchronized (SocketServer.class) {
				if (instance == null) {
					instance = new SocketServer();
				}
			}
		}
		return instance;
	}
	public void startServer(String host,int port,String charInfoOutPath) {
		bindhost = host;
		bindport = port;
		this.charInfoOutPath = charInfoOutPath;
		Configuration config = new Configuration();
        config.setHostname(host);
        config.setPort(port);

		socketIOServer = new SocketIOServer(config);
		SocketIONamespace socketIONamespace = null;
		
		socketIONamespace = socketIOServer.addNamespace("/" + HALL);
		socketIONamespace.addListeners(HallListener.getInstance());
		
		socketIONamespace = socketIOServer.addNamespace("/" + TALK);
		socketIONamespace.addListeners(TalkListener.getInstance());
		
		socketIOServer.start();
		LOGGER.info("socket启动成功，监听端口为：" + bindport);
	}
	
	/**
	 * 获取某个client
	 * @param sessionId
	 * @return
	 */
	public SocketIOClient findClientBySessionId(String sessionId){
		Collection<SocketIOClient> clients = socketIOServer.getAllClients();
		for (SocketIOClient client : clients) {
			if(sessionId.equals(client.getSessionId())){
				return client;
			}
		}
		return null;
	}
	
	public synchronized SocketIONamespace startChatRoom(String crName) {
		LOGGER.info("add name space:/" + crName);
		SocketIONamespace socketNameSpace = socketIOServer.addNamespace("/" + crName);
		Object ol = SpringBeanUtil.getBean(crName, Object.class);
		socketNameSpace.addListeners(ol);
        return socketNameSpace;
	}
	
	public void startServer(){
		startServer(bindhost, bindport, charInfoOutPath);
	}


	public void shutdown() {
		socketIOServer.stop();
	}
	public int getBindport() {
		return bindport;
	}
	public void setBindport(int bindport) {
		this.bindport = bindport;
	}
	public String getBindhost() {
		return bindhost;
	}
	public void setBindhost(String bindhost) {
		this.bindhost = bindhost;
	}
	public static String getCharInfoOutPath() {
		return charInfoOutPath;
	}
	public static void setCharInfoOutPath(String charInfoOutPath) {
		SocketServer.charInfoOutPath = charInfoOutPath;
	}

    public String getVisithost() {
        return visithost;
    }

    public void setVisithost(String visithost) {
        this.visithost = visithost;
    }
}
